package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author xixi
 * @date 2025-10-29
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //return getById(id);
        // 1.查询护理计划表
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //2. 查询护理计划 项目列表
       List<NursingProjectPlanVo> list= nursingProjectPlanMapper.selectNursingProjectPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 保存护理计划数据
        NursingPlan nursingplan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingplan);
        nursingplan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingplan);


        // 保存护理计划的护理项目信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
       int count= nursingProjectPlanMapper.batchInsert(projectPlans,nursingplan.getId());

        return count >0 ?1:0;

    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1. 修改 护理计划表
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        int count = nursingPlanMapper.updateNursingPlan(nursingPlan);

        //2. 修改 护理计划项目表
        // 2.1 先删除 中间表数据
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlan.getId());
        //2.2 批量新增
        nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(),nursingPlanDto.getId());


        return   count;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ?1 :0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id)
    {
        int count = nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
        return count;
    }
}
